try {
	Type.registerNamespace('NCMMPubUtil');
	//定义全局变量
	NCMMPubUtil.DatabaseName = "ncmmpda";
	NCMMPubUtil.PageSize = 500;
	//定义SuperVO
	NCMMPubUtil.SuperVO = function() {};
	NCMMPubUtil.SuperVO.prototype.getAttribute = function(key) {
		return this[key.name];
	};
	NCMMPubUtil.SuperVO.prototype.setAttribute = function(key, value) {
		this[key.name] = value;
	};
	NCMMPubUtil.SuperVO.prototype.getTableName = function() {
		return null;
	};
	NCMMPubUtil.SuperVO.prototype.getAllAttrs = function() {
		return null;
	};
	NCMMPubUtil.SuperVO.prototype.getPrimaryAttr = function() {
		return null;
	};
	//定义数据类型
	NCMMPubUtil.VALUE_TYPE_INTEGER = "INTEGER";
	NCMMPubUtil.VALUE_TYPE_REAL = "REAL";
	NCMMPubUtil.VALUE_TYPE_TEXT = "TEXT";
	//全局函数
	NCMMPubUtil.isEmpty = function(value) {
		if(value == null)
			return true;
		if(typeof(value) == "undefined")
			return true;
		if(value == "")
			return true;
		if(value instanceof Array && value.length == 0)
			return true;
		return false;
	};
	NCMMPubUtil.isNotEmpty = function(value) {
		return !NCMMPubUtil.isEmpty(value);
	};
	NCMMPubUtil.getNowDate = function() {
		var date = new Date();
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if(month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if(strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = date.getFullYear() + "-" + month + "-" + strDate;
		return currentdate;
	};
	NCMMPubUtil.getNowDateTime = function() {
		var date = new Date();
		var seperator1 = "-";
		var seperator2 = ":";
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		var hour = date.getHours();
		var minute = date.getMinutes();
		var second = date.getSeconds();

		if(month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if(strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		if(hour >= 0 && hour <= 9) {
			hour = "0" + hour;
		}
		if(minute >= 0 && minute <= 9) {
			minute = "0" + minute;
		}
		if(second >= 0 && second <= 9) {
			second = "0" + second;
		}
		var currentdate = date.getFullYear() + "-" + month + "-" + strDate + " " + hour + ":" + minute + ":" + second;
		return currentdate;
	};
	NCMMPubUtil.getTS = function() {
		var timestamp = (new Date()).valueOf();
		return timestamp;
	};

	NCMMPubUtil.notifyFilter = function(filterTextId, listViewId, filterFields) {
		var listViewBindf = $ctx.get("ncmm_qfilter_listbf");
		if(NCMMPubUtil.isEmpty(listViewBindf)) {
			var filterTxtBindf = $id(filterTextId).get("bindfield");
			listViewBindf = $id(listViewId).get("bindfield");
			$ctx.put("ncmm_qfilter_txtbf", filterTxtBindf);
			$ctx.put("ncmm_qfilter_listbf", listViewBindf);
			$ctx.put("ncmm_qfilter_fields", filterFields);
		}
		var datas = $ctx.getJSONArray(listViewBindf);
		$ctx.put("ncmm_qfilter_databackup", datas);
		$ctx.dataBind();

	};
	NCMMPubUtil.quickFilter = function() {
		$ctx.dataCollect();
		var filterTxtBindf = $ctx.get("ncmm_qfilter_txtbf");
		var listViewBindf = $ctx.get("ncmm_qfilter_listbf");
		var filtertxt = $ctx.getString(filterTxtBindf);
		var filterFields = null;
		if(NCMMPubUtil.isNotEmpty($ctx.get("ncmm_qfilter_fields"))) {
			filterFields = $ctx.getJSONArray("ncmm_qfilter_fields");
		}
		var databackup = null;
		if(NCMMPubUtil.isNotEmpty($ctx.get("ncmm_qfilter_databackup"))) {
			databackup = $ctx.getJSONArray("ncmm_qfilter_databackup");
		}
		if(NCMMPubUtil.isNotEmpty(filtertxt)) {
			var resultdatas = new Array();
			if(NCMMPubUtil.isNotEmpty(databackup)) {
				for(var i = 0; i < databackup.length; i++) {
					var contained = false;
					if(NCMMPubUtil.isNotEmpty(filterFields)) {
						for(var j = 0; j < filterFields.length; j++) {
							var fieldvalue = databackup[i][filterFields[j]];
							if(fieldvalue.indexOf(filtertxt) >= 0) {
								contained = true;
								break;
							}
						}
					} else {
						for(field in databackup[i]) {
							var fieldvalue = databackup[i][field];
							if(fieldvalue.indexOf(filtertxt) >= 0) {
								contained = true;
								break;
							}
						}
					}
					if(contained) {
						resultdatas.push(databackup[i]);
					}
				}
			}
			$ctx.put(listViewBindf, resultdatas);
			$ctx.dataBind();
		} else {
			$ctx.put(listViewBindf, databackup);
			$ctx.dataBind();
		}
	};
	//数据库操作
	NCMMPubUtil.createTable = function(VOClass) {
		var tblname = VOClass.prototype.getTableName();
		var cols = VOClass.prototype.getAllAttrs();
		var sql = "create table if not exists " + tblname + "(";
		for(var i = 0; i < cols.length; i++) {
			sql = sql + cols[i].name + " " + cols[i].type + ",";
		}
		sql = sql.slice(0, -1);
		sql += ")";
		//		$alert("create sql="+sql);
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		};
		$sqlite.execSql(param);
	};
	//数据库操作
	NCMMPubUtil.createTableWithAutoPK = function(VOClass) {
		var tblname = VOClass.prototype.getTableName();
		var cols = VOClass.prototype.getAllAttrs();
		var pkcol = VOClass.prototype.getPrimaryAttr();
		var sql = "create table if not exists " + tblname + "(";
		for(var i = 0; i < cols.length; i++) {
			if(pkcol == cols[i].name) {
				sql = sql + cols[i].name + " " + cols[i].type + " PRIMARY KEY AUTOINCREMENT,";
			} else {
				sql = sql + cols[i].name + " " + cols[i].type + ",";
			}
		}
		sql = sql.slice(0, -1);
		sql += ")";
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		};
		$sqlite.execSql(param);
	};
	NCMMPubUtil.dropTable = function(VOClass) {
		var tblname = VOClass.prototype.getTableName();
		var sql = "drop table if exists " + tblname;
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		$sqlite.execSql(param);
	};

	NCMMPubUtil.insertVO = function(VOClass, vo) {
		vo = NCMMPubUtil.fillAttributes(VOClass, vo);
		var tblname = VOClass.prototype.getTableName();
		var cols = VOClass.prototype.getAllAttrs();
		var colvalues = "";
		var sql = "insert into " + tblname + " ("
		for(var i = 0; i < cols.length; i++) {
			sql = sql + cols[i].name + ",";
			var colvalue = vo[cols[i].name];
			if(colvalue != null && typeof(colvalue) == "string" && cols[i].type == NCMMPubUtil.VALUE_TYPE_TEXT) {
				colvalues = colvalues + "'" + NCMMPubUtil.htmlspecialchars2sqlite(colvalue) + "',";
			} else {
				colvalues = colvalues + colvalue + ",";
			}
			//			if(cols[i].type == NCMMPubUtil.VALUE_TYPE_INTEGER || cols[i].type == NCMMPubUtil.VALUE_TYPE_REAL) {
			//				colvalues = colvalues + colvalue + ",";
			//			} else if(cols[i].type == NCMMPubUtil.VALUE_TYPE_TEXT) {
			//				colvalues = colvalues + "'" + NCMMPubUtil.htmlspecialchars2sqlite(colvalue) + "',";
			//			}
		}
		sql = sql.slice(0, -1);
		colvalues = colvalues.slice(0, -1);
		sql = sql + ") values(" + colvalues + ")";
		//$alert("insertvo sql=" + sql);
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		$sqlite.execSql(param);
	};

	// insertBatchVO 批量新增
	NCMMPubUtil.insertBatchVO = function(VOClass, billinfos) {
		// 表名
		var tablename = VOClass.prototype.getTableName();
		// 列名
		var columns = VOClass.prototype.getAllAttrs();
		var cols = Array.prototype.map.call(columns, function(col, i, arr) {
			return col.name;
		});
		//		$alert("before insertBatch="+$jsonToString(billinfos));
		// 拼插入数据
		var sql = "INSERT INTO " + tablename + "(" + cols.join(',') + ") VALUES";
		for(var i = 0; i < billinfos.length; i++) {
			var billinfo = billinfos[i].billinfo;
			if(billinfo == null)
				billinfo = billinfos[i];
			var vo = NCMMPubUtil.fillAttributes(VOClass, billinfo);

			for(var k = 0; k < (VOClass.prototype.getAllAttrs()).length; k++) {
				var attr = (VOClass.prototype.getAllAttrs())[k];
				if(!!vo[attr.name] && vo[attr.name] != "" && typeof(vo[attr.name]) == "string") {
					vo[attr.name] = NCMMPubUtil.htmlspecialchars2sqlite(vo[attr.name]);
				}
			}

			var vals = Array.prototype.map.call(columns, function(col, i, arr) {
				if(columns[i].type == NCMMPubUtil.VALUE_TYPE_INTEGER || columns[i].type == NCMMPubUtil.VALUE_TYPE_REAL) {
					return "" + vo[col.name] + "";
				} else if(columns[i].type == NCMMPubUtil.VALUE_TYPE_TEXT) {
					return "'" + vo[col.name] + "'";
				}
			});
			sql = sql + "(" + vals.join(',') + "),";
		}
		sql = sql.slice(0, -1);
		//$alert(sql);
		$sqlite.execSql({
			"db": NCMMPubUtil.DatabaseName,
			"sql": sql
		});
		sql = "select count(*) count from " + tablename;
		var retquery = $sqlite.query({
			"db": NCMMPubUtil.DatabaseName,
			"sql": sql
		})
		var retcount = parseInt((eval(retquery)[0])["count"]);
		return retcount;
	};

	NCMMPubUtil.updateVO = function(VOClass, vo) {
		vo = NCMMPubUtil.fillAttributes(VOClass, vo);
		//		$alert("updatevo="+$jsonToString(vo));
		var tblname = VOClass.prototype.getTableName();
		var cols = VOClass.prototype.getAllAttrs();
		var pkcol = VOClass.prototype.getPrimaryAttr();
		var sql = "update " + tblname + " set ";
		for(var i = 0; i < cols.length; i++) {
			if(cols[i].name == pkcol.name) {
				continue;
			}
			var colvalue = vo[cols[i].name];
			if(colvalue != null && typeof(colvalue) == "string" && cols[i].type == NCMMPubUtil.VALUE_TYPE_TEXT) {
				colvalue = "'" + NCMMPubUtil.htmlspecialchars2sqlite(colvalue) + "'";
			}
			sql = sql + cols[i].name + "=" + colvalue + ",";
		}
		sql = sql.slice(0, -1);
		var pkcolvalue = vo[pkcol.name];
		if(pkcolvalue != null && typeof(pkcolvalue) == "string" && pkcol.type == NCMMPubUtil.VALUE_TYPE_TEXT) {
			pkcolvalue = "'" + NCMMPubUtil.htmlspecialchars2sqlite(pkcolvalue) + "'";
		}
		sql = sql + " where " + pkcol.name + "=" + pkcolvalue;
		//$alert("update sql = " + sql);
		var param = {
				db: NCMMPubUtil.DatabaseName,
				sql: sql
			}
			//		$alert("update sql param="+$jsonToString(param));
		$sqlite.execSql(param);
	};
	NCMMPubUtil.updateVOWithAttris = function(VOClass, vo, attris) {
		vo = NCMMPubUtil.fillAttributes(VOClass, vo);
		//		$alert("updatevo="+$jsonToString(vo));
		var tblname = VOClass.prototype.getTableName();
		var cols = attris //VOClass.prototype.getAllAttrs();
		var pkcol = VOClass.prototype.getPrimaryAttr();
		var sql = "update " + tblname + " set ";
		for(var i = 0; i < cols.length; i++) {
			if(cols[i].name == pkcol.name) {
				continue;
			}
			var colvalue = vo[cols[i].name];
			if(colvalue != null && typeof(colvalue) == "string" && cols[i].type == NCMMPubUtil.VALUE_TYPE_TEXT) {
				colvalue = "'" + NCMMPubUtil.htmlspecialchars2sqlite(colvalue) + "'";
			}
			sql = sql + cols[i].name + "=" + colvalue + ",";
		}
		sql = sql.slice(0, -1);
		var pkcolvalue = vo[pkcol.name];
		if(pkcolvalue != null && typeof(pkcolvalue) == "string" && pkcol.type == NCMMPubUtil.VALUE_TYPE_TEXT) {
			pkcolvalue = "'" + NCMMPubUtil.htmlspecialchars2sqlite(pkcolvalue) + "'";
		}
		sql = sql + " where " + pkcol.name + "=" + pkcolvalue;
		//$alert("update sql = " + sql);
		var param = {
				db: NCMMPubUtil.DatabaseName,
				sql: sql
			}
			//		$alert("update sql param="+$jsonToString(param));
		$sqlite.execSql(param);
	};

	NCMMPubUtil.updateVOWithOutPK = function(VOClass, vo, rowid) {
		vo = NCMMPubUtil.fillAttributes(VOClass, vo);
		var tblname = VOClass.prototype.getTableName();
		var cols = VOClass.prototype.getAllAttrs();
		var pkcol = VOClass.prototype.getPrimaryAttr();
		var sql = "update " + tblname + " set ";
		for(var i = 0; i < cols.length; i++) {
			if(cols[i].name == pkcol.name) {
				continue;
			}
			var colvalue = vo[cols[i].name];
			if(colvalue != null && typeof(colvalue) == "string" && cols[i].type == NCMMPubUtil.VALUE_TYPE_TEXT) {
				colvalue = "'" + NCMMPubUtil.htmlspecialchars2sqlite(colvalue) + "'";
			}
			sql = sql + cols[i].name + "=" + colvalue + ",";
		}
		sql = sql.slice(0, -1);
		var pkcolvalue = rowid;
		sql = sql + " where rowid =" + pkcolvalue;
		//		$alert("update sql = " + sql);
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		$sqlite.execSql(param);
	};

	NCMMPubUtil.deleteVO = function(VOClass, vo) {
		var tblname = VOClass.prototype.getTableName();
		var sql = "delete from " + tblname
		var pkcol = VOClass.prototype.getPrimaryAttr();
		var pkcolvalue = vo[pkcol.name];
		if(pkcolvalue != null && typeof(pkcolvalue) != "undefined" && pkcol.type == NCMMPubUtil.VALUE_TYPE_TEXT) {
			pkcolvalue = "'" + pkcolvalue + "'";
		}
		sql = sql + " where " + pkcol.name + "=" + pkcolvalue;
		//		$alert("delete sql="+sql);
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		$sqlite.execSql(param);
	};
	//增加删除某张表中数据
	NCMMPubUtil.deleteAll = function(VOClass) {
		var tblname = VOClass.prototype.getTableName();
		var sql = "delete from " + tblname
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		$sqlite.execSql(param);
	};

	//批量删除某张表中符合条件的所有数据
	NCMMPubUtil.batchDeleteByCondition = function(VOClass, condition) {
		var tblname = VOClass.prototype.getTableName();
		var sql = "delete from " + tblname;
		if(condition == null) {
			NCMMPubUtil.deleteAll(VOClass);
			return;
		}
		sql = sql + " where " + condition;
		//		$alert("sql = " + sql);
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		$sqlite.execSql(param);
	};

	NCMMPubUtil.queryVONoCallback = function(VOClass, wheresql, pageIndex, pageSize) {
		if(NCMMPubUtil.isTableExist(VOClass) == false)
			return null;
		if(pageIndex == null)
			pageIndex = 0;

		if(pageSize == null) {
			var rowsum = NCMMPubUtil.queryVOCount(VOClass, wheresql, null);
			if(rowsum == null || rowsum == NaN) {
				pageSize = NCMMPubUtil.PageSize;
			} else {
				pageSize = rowsum;
			}
		}

		var tblname = VOClass.prototype.getTableName();
		var cols = VOClass.prototype.getAllAttrs();
		if(wheresql == null) {
			wheresql = "";
		}
		var sql = "select * from " + tblname + " " + wheresql;
		var param = {
				db: NCMMPubUtil.DatabaseName,
				sql: sql,
				pageIndex: pageIndex,
				pageSize: pageSize
			}
			//		if(tblname == "ic_materialout" || tblname == "ic_materialout_scans")
			//				$alert("queryVO param="+$jsonToString(param));
		var data = $sqlite.queryByPage(param); //typeof(data) == "object"
		//		$alert("data type = "+ typeof(data));
		//		$alert("data="+jsonToString(data));
		return data;
	};

	NCMMPubUtil.queryVO = function(VOClass, wheresql, callback, pageIndex, pageSize) {
		if(NCMMPubUtil.isTableExist(VOClass) == false)
			return null;
		if(pageIndex == null)
			pageIndex = 0;

		if(pageSize == null) {
			var rowsum = NCMMPubUtil.queryVOCount(VOClass, wheresql, null);
			if(rowsum == null || rowsum == NaN) {
				pageSize = NCMMPubUtil.PageSize;
			} else {
				pageSize = rowsum;
			}
		}
		var tblname = VOClass.prototype.getTableName();
		var cols = VOClass.prototype.getAllAttrs();
		if(wheresql == null) {
			wheresql = "";
		}
		var sql = "select * from " + tblname + " " + wheresql;
		var param = {
				db: NCMMPubUtil.DatabaseName,
				sql: sql,
				pageIndex: pageIndex,
				pageSize: pageSize
			}
			//		$alert("queryVO param="+$jsonToString(param));
		var data = $sqlite.queryByPage(param);
		var datatype = typeof(data);
		if(datatype == "string") {
			data = $stringToJSON(data);
			//			$alert("after to json data=" + JSON.stringify(data));
		}

		if(callback != null && callback != undefined && typeof(callback) == 'function') {
			callback(data);
		}
	};
	NCMMPubUtil.queryVOByConditions = function(VOClass, conditions, callback, pageIndex, pageSize) {
		if(NCMMPubUtil.isTableExist(VOClass) == false)
			return null;
		var wheresql = NCMMPubUtil.parseQueryConditions(conditions);
		//		$alert("wheresql: "+ wheresql);
		if(wheresql != "") {
			wheresql = " where " + wheresql;
		}
		if(pageIndex == null)
			pageIndex = 0;

		if(pageSize == null) {
			var rowsum = NCMMPubUtil.queryVOCount(VOClass, wheresql, null);
			if(rowsum == null || rowsum == NaN) {
				pageSize = NCMMPubUtil.PageSize;
			} else {
				pageSize = rowsum;
			}
		}
		NCMMPubUtil.queryVO(VOClass, wheresql, callback, pageIndex, pageSize);
	};

	NCMMPubUtil.parseQueryConditions = function(conditions) {
		var wheresql = "";
		for(key in conditions) {
			var value = conditions[key];
			var keys = key.split("_");
			var truekey = keys[0];
			var qrysymbol = null;
			if(keys.length > 1) {
				qrysymbol = keys[keys.length - 1];
			}
			if(value == null || typeof(value) == "undefined" || value == "") {
				if(qrysymbol == "datecrtday" || qrysymbol == "datecrtweek" || qrysymbol == "datecrtmonth") {
					value = NCMMPubUtil.getNowDate();
				} else {
					continue;
				}
			}
			switch(qrysymbol) {
				case "leftlike":
					value = " like '" + value + "%' ";
					break;
				case "rightlike":
					value = " like '%" + value + "' ";
					break;
				case "fulllike":
					value = " like '%" + value + "%' ";
					break;
				case "less":
					value = " < " + value + " ";
					break;
				case "greater":
					value = " > " + value + " ";
					break;
				case "lseq":
					value = " <= " + value + " ";
					break;
				case "grteq":
					value = " >= " + value + " ";
					break;
				case "unequal":
					value = " <> '" + value + "' ";
					break;
				case "timeeq":
					truekey = " strftime('%s'," + keys[0] + ") ";
					value = " - strftime('" + value + "')=0 ";
					break;
				case "timels":
					truekey = " strftime('%s'," + keys[0] + ") ";
					value = " - strftime('" + value + "')<0 ";
					break;
				case "timegrt":
					truekey = " strftime('%s'," + keys[0] + ") ";
					value = " - strftime('" + value + "')>0 ";
					break;
				case "timelseq":
					truekey = " strftime('%s'," + keys[0] + ") ";
					value = " - strftime('" + value + "')<=0 ";
					break;
				case "timegrteq":
					truekey = " strftime('%s'," + keys[0] + ") ";
					value = " - strftime('" + value + "')>=0 ";
					break;
				case "dateeq":
					truekey = " julianday(" + keys[0] + ") ";
					value = " - julianday('" + value + "')=0 ";
					break;
				case "datecrtday":
					truekey = " julianday(" + keys[0] + ") ";
					value = " - julianday('" + value + "')=0 ";
					break;
				case "datels":
					truekey = " julianday(" + keys[0] + ") ";
					value = " - julianday('" + value + "')<0 ";
					break;
				case "dategrt":
					truekey = " julianday(" + keys[0] + ") ";
					value = " - julianday('" + value + "')>0 ";
					break;
				case "datelseq":
					truekey = " julianday(" + keys[0] + ") ";
					value = " - julianday('" + value + "')<=0 ";
					break;
				case "dategrteq":
					truekey = " julianday(" + keys[0] + ") ";
					value = " - julianday('" + value + "')>=0 ";
					break;
				case "dateweekin":
					truekey = " julianday(" + keys[0] + ") - julianday('" + value + "')>=0 and";
					value = " julianday(" + keys[0] + ") - julianday('" + value + "')<=7";
					break;
				case "datecrtweek":
					truekey = " strftime('%Y'," + keys[0] + ")-strftime('%Y','" + value + "')=0 and ";
					value = " strftime('%W'," + keys[0] + ")-strftime('%W','" + value + "')=0 ";
					break;
				case "datemonthin":
					truekey = " julianday(" + keys[0] + ") - julianday('" + value + "')>=0 and";
					value = " julianday(" + keys[0] + ") - julianday('" + value + "')<=30";
					break;
				case "datecrtmonth":
					truekey = " strftime('%Y'," + keys[0] + ")-strftime('%Y','" + value + "')=0 and ";
					value = " strftime('%m'," + keys[0] + ")-strftime('%m','" + value + "')=0 ";
					break;
				default:
					if(typeof(value) == "string") {
						value = "='" + value + "' ";
					} else {
						value = "=" + value + " "
					}
			}
			wheresql = wheresql + " " + truekey + value + " and";
		}
		if(wheresql != "") {
			wheresql = wheresql.slice(0, -3);
		}
		return wheresql;
	};
	NCMMPubUtil.queryVOCount = function(VOClass, wheresql, callback) {
		var tblname = VOClass.prototype.getTableName();
		if(wheresql == null) {
			wheresql = "";
		}
		var sql = "select count(*) as nums from " + tblname + " " + wheresql;
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql,
			pageIndex: 0,
			pageSize: NCMMPubUtil.PageSize
		}
		var data = $sqlite.queryByPage(param);
		data = $stringToJSON(data);
		var nums = data[0]["nums"];
		if(callback != null && callback != undefined && typeof(callback) == 'function') {
			callback(nums);
		} else {
			return nums;
		}
	}

	//NC端没传过来的字段值保存到本地表里会保存成undefined，所以在保存前做一下处理
	NCMMPubUtil.fillAttributes = function(VOClass, vo) {
		for(var k = 0; k < (VOClass.prototype.getAllAttrs()).length; k++) {
			var attr = (VOClass.prototype.getAllAttrs())[k];
			if(vo.hasOwnProperty(attr.name) == false || vo[attr.name] == null) {
				vo[attr.name] = "";
			}
		}
		return vo;
	};

	/**
	 *根据条件查询特定的一列，需要传含有where的where语句
	 * @param {Object} VOClass
	 * @param {Object} OneCol
	 * @param {Object} wheresql
	 */
	NCMMPubUtil.queryVORtnOneCol = function(VOClass, OneCol, wheresql) {
			var tblname = VOClass.prototype.getTableName();
			if(wheresql == null) {
				wheresql = "";
			}
			var colname = OneCol.name;
			var sql = "select " + colname + " from " + tblname + " " + wheresql;

			var param = {
				db: NCMMPubUtil.DatabaseName,
				sql: sql,
				pageIndex: 0,
				pageSize: NCMMPubUtil.PageSize
			}
			var data = $sqlite.queryByPage(param);
			data = $stringToJSON(data);
			//$alert("data="+$jsonToString(data));
			return data;
		}
		// 获取系统业务设置的库存组织和仓库
	NCMMPubUtil.getDefaultCondition = function() {
		var json = {};
		var stockorg = $cache.read("defaultstockorg") ? $cache.read("defaultstockorg") : "";
		if(stockorg != "") {
			stockorg = $stringToJSON(stockorg);
			stockorg = stockorg["code"];
		}
		var stordoc = $cache.read("defaultstordoc") ? $cache.read("defaultstordoc") : "";
		if(stordoc != "") {
			stordoc = $stringToJSON(stordoc);
			stordoc = stordoc["code"];
		}
		if(stockorg == "" || stordoc == "") {
			$toast("下载前请先进行业务设置");
			return null;
		}
		json["stockorg"] = stockorg;
		json["stordoc"] = stordoc;
		return json;
	}

	NCMMPubUtil.getSumScanedNum = function(scanedNum, currentScanedNum) {
		if(scanedNum == null || scanedNum == "") {
			scanedNum += "0.00000";
		} else {
			scanedNum = parseFloat(scanedNum);
		}
		if(currentScanedNum == null || currentScanedNum == "") {
			currentScanedNum += "1.00000";
		} else {
			currentScanedNum = parseFloat(currentScanedNum);
		}
		var newScanedNum = parseFloat(scanedNum).add(parseFloat(currentScanedNum));
		return newScanedNum;
	}

	NCMMPubUtil.getSubScanedNum = function(scanedNum, currentScanedNum) {
			if(scanedNum == null || scanedNum == "") {
				scanedNum += "0.00000";
			} else {
				scanedNum = parseFloat(scanedNum);
			}
			if(currentScanedNum == null || currentScanedNum == "") {
				currentScanedNum += "1.00000";
			} else {
				currentScanedNum = parseFloat(currentScanedNum);
			}
			var newScanedNum = parseFloat(scanedNum) - (parseFloat(currentScanedNum));
			return newScanedNum;
		}
		//判断表是否存在
	NCMMPubUtil.isTableExist = function(VOClass) {
		var sql = "select count(*)  count from sqlite_master where type='table' and name='" + VOClass.prototype.getTableName() + "'";
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql,
			pageIndex: 0,
			pageSize: NCMMPubUtil.PageSize
		}

		var data = $sqlite.queryByPage(param);
		data = eval(data);
		if(NCMMPubUtil.isEmpty(data)) {
			return false;
		}
		data = parseInt(data[0]["count"]);
		if(data > 0)
			return true;
		else
			return false;
	};

	//自定义查询
	NCMMPubUtil.queryBySQL = function(sql) {
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		var data = $sqlite.query(param);
		//		$alert("query result="+data);
		data = eval(data);
		return data;
	};

	NCMMPubUtil.isScanVOExistByPK = function(VOClass, pkvalue) {
		var tablename = VOClass.prototype.getTableName();
		var pkname = VOClass.prototype.getPrimaryAttr().name;
		var sql = "select " + pkname + " from " + tablename + " where " + pkname + " = '" + pkvalue + "'";
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql,
			pageIndex: 0,
			pageSize: NCMMPubUtil.PageSize
		}
		var data = $sqlite.queryByPage(param);
		if(data != null && eval(data).length > 0) {
			return true;
		}
		return false;
	};

	//NC端没传过来的字段值如果包含单双引号，保存到本地表时会出错，所以要提前转义
	NCMMPubUtil.processSpecialChars = function(VOClass, vo) {
		for(var k = 0; k < (VOClass.prototype.getAllAttrs()).length; k++) {
			var attr = (VOClass.prototype.getAllAttrs())[k];
			if(!!vo[attr.name] && vo[attr.name] != "" && typeof(vo[attr.name]) == "string") {
				vo[attr.name] = NCMMPubUtil.htmlspecialchars(vo[attr.name]);
			}
		}
		return vo;
	};

	NCMMPubUtil.htmlspecialchars2sqliteex = function(s) {
		if(!s || typeof(s) != 'string') {
			return s;
		}
		s = s + '#';
		s = s.replace(/['"\r\n\\]?/mg, function(tokens, index, currentStr) {
			if(tokens.charCodeAt(0) == 34) {
				return '\\' + tokens; //双引号
			} else if(tokens.charCodeAt(0) == 39) {
				return '\\' + tokens + '\\' + tokens; //单引号
			} else if(tokens.charCodeAt(0) == 13) {
				return '\\r';
			} else if(tokens.charCodeAt(0) == 10) {
				return '\\n';
			} else if(tokens.charCodeAt(0) == 0) {
				return '\\0';
			} else if(tokens.charCodeAt(0) == 92) {
				return '\\\\';
			}
			return tokens;
		});
		s = s.substring(0, s.length - 1);
		return s;
	};

	NCMMPubUtil.htmlspecialchars = function(s) {
		var len = s.length;
		var ss = "";
		for(var i = 0; i < len; i++) {
			if(s.charCodeAt(i) == 34) {
				ss = ss + '\\' + s.charAt(i);
			} else if(s.charCodeAt(i) == 39) {
				ss = ss + '\\' + s.charAt(i) + '\\' + s.charAt(i);
			} else if(s.charCodeAt(i) == 13) {
				ss = ss + '\\r';
			} else if(s.charCodeAt(i) == 10) {
				ss = ss + '\\n';
			} else if(s.charCodeAt(i) == 0) {
				ss = ss + '\\0';
			} else if(s.charCodeAt(i) == 92) {
				if(i == len - 1 || s.charAt(i + 1) == '\\' || s.charAt(i + 1) == '\"' || s.charAt(i + 1) == 'r' || s.charAt(i + 1) == 'n' || s.charAt(i + 1) == '0')
					ss = ss + '\\\\\\\\';
				else if(i > 1 && s.charAt(i - 1) == '\\')
					ss = ss + '\\\\\\\\';
				else
					ss = ss + '\\\\';
			} else {
				ss = ss + s.charAt(i);
			}
		}

		return ss;
	};

	//处理显示时的特殊字符 -- 数组
	NCMMPubUtil.htmlspecialjsonarray2UI = function(dataarray) {
		for(var i = 0; i < dataarray.length; i++) {
			if(typeof(dataarray[i]) == "string") {
				dataarray[i] = NCMMPubUtil.htmlspecialchars2UI(dataarray[i]);
			} else {
				dataarray[i] = NCMMPubUtil.htmlspecialjson2UI(dataarray[i]);
			}
		}

		return dataarray;
	};

	//处理显示时的特殊字符 -- json
	NCMMPubUtil.htmlspecialjson2UI = function(jsondata) {
		for(var attr in jsondata) {
			jsondata[attr] = NCMMPubUtil.htmlspecialchars2UI(jsondata[attr]);
		}

		return jsondata;
	};

	//处理显示时的特殊字符 -- string
	NCMMPubUtil.htmlspecialchars2UI = function(s) {
		if(s == null || s == NaN)
			return s;

		if(typeof(s) == "string" && s != "" && s.indexOf('\"') < 0) {
			s = s.replace(/[\\]?/mg, function(tokens, index, currentStr) {
				if(tokens.charCodeAt(0) == 92) {
					return '\\\\';
				}
				return tokens;
			});
		}

		return s;
	};

	//插入到sqlite时特殊符号处理
	NCMMPubUtil.htmlspecialchars2sqlite = function(s) {
		//		s = s + '#';
		s = s.replace(/[']?/mg, function(tokens, index, currentStr) {
			//单引号
			if(tokens.charCodeAt(0) == 39) {
				//return '\\' + tokens + '\\' + tokens;
				return tokens + tokens;
			}
			return tokens;
		});
		//		s = s.substring(0, s.length - 1);
		return s;
	};
	// 打开参照页面
	NCMMPubUtil.openRefPage = function(ncbillcode, ctxnamekey, ctxpkkey, pkfiel, wheresql) {
		var jsonparam = {};
		getRefParam(ncbillcode, function(data) {
			jsonparam = data;
		});

		if(jsonparam == null) {
			//$alert("jsonparam is null!");
			return;
		}
		jsonparam["wheresql"] = wheresql;
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.RefPage", //目标页面
			isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			data: jsonparam,
			callback: function() {
				var refdata = $param.getJSONObject("data");
				$ctx.put(ctxnamekey, refdata["name"]);
				$ctx.put(ctxpkkey, refdata[pkfiel]);
				$ctx.dataBind();
				$cache.write("default" + ctxnamekey, $jsonToString(refdata));

				//设置了默认库存组织后，自动下载默认库存组织下的仓库
				//where pk_org='库存组织编码'
				if(ctxnamekey == "stockorg") {
					var condition = {
						"pk_org": "='" + refdata["code"] + "'"
					};
					processDocDataFromNC(StordocVO, condition);
				}
			}
		});
	}
	NCMMPubUtil.openBilltypeRefPage = function(ctxpkkey, ctxnamekey, billtype) {
		var jsonparam = {};
		getRefParam("PDABILLTYPE", function(data) {
			jsonparam = data;
		});

		if(jsonparam == null) {
			return;
		}

		jsonparam["wheresql"] = " where parentbilltype='" + billtype + "'";
		console.log(jsonparam["wheresql"]);
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.RefPage", //目标页面
			isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			data: jsonparam,
			callback: function() {
				var refdata = $param.getJSONObject("data");
				$ctx.put(ctxnamekey, refdata["name"]);
				$ctx.put(ctxpkkey, refdata["pk_billtypeid"]);
				$ctx.dataBind();
			}
		});
	}
	NCMMPubUtil.openAstUnitRefPage = function(ctxnamekey, ctxpkkey, ctxchangratekey, materialID,numkey,astnumkey) {
		var jsonparam = {};
		getRefParam("PDAMATERIALCONVERT", function(data) {
			jsonparam = data;
		});

		if(jsonparam == null) {
			//$alert("jsonparam is null!");
			return;
		}
		jsonparam["wheresql"] = "where pk_material='" + materialID + "'";
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.AstUnitRefPage", //目标页面
			isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			data: jsonparam,
			callback: function() {
				var refdata = $param.getJSONObject("data");
				$ctx.put(ctxnamekey, refdata["measdoc_name"]);
				$ctx.put(ctxpkkey, refdata["pk_measdoc"]);
				$ctx.put(ctxchangratekey, refdata["vchangerate"]);
				if(!NCMMPubUtil.isEmpty($ctx.get(numkey))){
				var nastnum=NCMMPubUtil.getAstnum($ctx.get(numkey),refdata["vchangerate"]);
				var bitnum=NCMMPubUtil.getUnitBitnumber(refdata["pk_measdoc"]);
				if(!NCMMPubUtil.isEmpty(bitnum)) {
				   nastnum = nastnum.toFixed(bitnum);
			     }
				$ctx.put(astnumkey,nastnum);
				}
				$ctx.dataBind();
				$cache.write("default" + ctxnamekey, $jsonToString(refdata));

				//设置了默认库存组织后，自动下载默认库存组织下的仓库
				//where pk_org='库存组织编码'
				if(ctxnamekey == "stockorg") {
					var condition = {
						"pk_org": "='" + refdata["code"] + "'"
					};
					processDocDataFromNC(StordocVO, condition);
				}
			}
		});
	}

	//获取参照vo的vo信息
	//reftype： 外系统调用标识
	function getRefParam(reftype, callback) {
		var condition = {
			"ncbillcode": reftype
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
			if((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
				$alert("【" + reftitle + "】没有可参照的数据");
				if(callback != null && callback != undefined && typeof(callback) == 'function') {
					callback(null);
				}
				return;
			}
			var reftablename = (jsondata.bdlistdata)[0]["apptablename"]; //本地表名
			var jsonparam = {
				reftype: reftype, //外系统调用标识
				reftitle: reftitle, //元数据显示名称
				reftablename: reftablename //本地表名
			};
			if(callback != null && callback != undefined && typeof(callback) == 'function') {
				callback(jsonparam);
			}
		});
	}
	NCMMPubUtil.batchInsertTableVOs = function(VOClass, vos) {
		if(NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		var attrs = VOClass.prototype.getAllAttrs();

		var fields = attrs.map(function(row, i, arr) {
			return row.name;
		});
		//$alert("fields=" + $jsonToString(fields));
		var valueMapList = Array.prototype.map.call(vos, function(row, i, arr) {
			var billinfo_local = $stringToJSON((row || {}));
			var values = attrs.map(function(attr, i, array) {
				var fname = attr.name;
				if(!!billinfo_local[fname]) {
					if(billinfo_local[fname] != "" && typeof(billinfo_local[fname]) == "string") {
						billinfo_local[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(billinfo_local[fname]);
					}
					return billinfo_local[fname];
				} else {
					if(attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
						return 0;
					} else if(attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
						return 0.0;
					} else {
						return "";
					}
				}
			});
			//$alert("values="+$jsonToString(values))
			return values;
		});

		var sum = valueMapList.length;
		//$alert("valueMapList=" + $jsonToString(valueMapList));

		$app.replaceBatch({
			db: NCMMPubUtil.DatabaseName,
			table: VOClass.prototype.getTableName(),
			fields: fields,
			values: valueMapList,
			callback: function(args_null, args) {}
		});
		return sum;

	};
	/**
	 * 批量跟新数据
	 * @param {Object} VOClass 类
	 * @param {Object} vos 待更新vo数组
	 * @param {Object} unUpdateFieldanme 表中那一组列名不更新，字符串数组数组
	 */
	NCMMPubUtil.updateBatchVOs = function(VOClass, vos, scanedname) {
		if(NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		var attrs = VOClass.prototype.getAllAttrs();
		var fields = [];
		for(var i = 0; i < attrs.length; i++) {
			if(attrs[i].name != scanedname) {
				fields.push(attrs[i].name);
			}
		}
		//$alert("fields="+$jsonToString({fields:fields}));
		var values = [];
		for(var j = 0; j < vos.length; j++) {
			var row = $stringToJSON((vos[j] || {}));
			var rowVaules = [];
			for(var k = 0; k < attrs.length; k++) {
				var fname = attrs[k].name;
				var type = attrs[k].type;
				if(fields != scanedname) {
					if(!!row[fname]) {
						if(row[fname] != "" && typeof(row[fname]) == "string") {
							row[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(row[fname]);
						}
						rowVaules.push(row[fname]);
					} else {
						if(type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
							rowVaules.push(0);
						} else if(type == NCMMPubUtil.VALUE_TYPE_REAL) {
							rowVaules.push(0.0);

						} else {
							rowVaules.push("");
						}
					}
				}
			}
			values.push(rowVaules);

		}
		var sum = rowVaules.length;
		//$alert("rowVaules=" + $jsonToString(values));

		$app.replaceBatch({
			db: NCMMPubUtil.DatabaseName,
			table: VOClass.prototype.getTableName(),
			fields: fields,
			values: values,
			callback: function(args_null, args) {
				//$alert(args_null);
				//$alert(args);
			}
		});
		return sum;

	};
	NCMMPubUtil.getUnitBitnumber = function(unitid) {
		//更新发货单入库列表增加扫描数量 
		var wheresql = "where pk_measdoc='" + unitid + "'";
		var bitnumber = 0;
		if(!NCMMPubUtil.isTableExist(MeasdocVO)) {
			return;
		}
		NCMMPubUtil.queryVO(MeasdocVO, wheresql, function(data) {
			if(typeof(data) == 'undefined' || data == null || data.length == 0) {
				return;
			}
			var unitData = eval(data);
			if(unitData.length > 0) {
				bitnumber = unitData[0].bitnumber;
			} else {
				return;
			}
		});
		return bitnumber;
	};
	NCMMPubUtil.isNumber = function(inputstr) {
		var zregex = /^\d+(\.\d*)?$/;
		var fregex = /^((-\d*(\.\d*)?)|(0+(\.0*)?))$/;
		if(inputstr == null || inputstr == "") {
			return false;
		}
		if(!zregex.test(inputstr) && !fregex.test(inputstr)) {
			return false;
		}
		return true;
	}
	NCMMPubUtil.isBsBitnumber = function(num, unitid) {
		var unitBitnum = NCMMPubUtil.getUnitBitnumber(unitid);
		if(NCMMPubUtil.isEmpty(unitBitnum)) {
			return false;
		}
		if(num.split(".").length == 1) {
			return false;
		}
		var numBitNum = num.split(".")[1].length;
		if(numBitNum > unitBitnum) {
			return true;
		}
		return false;
	};
	NCMMPubUtil.getAddNum = function(num1, num2) {
		if(num1 == null || num1 == "") {
			num1 += "00.00000";
		} else {
			num1 = parseFloat(num1);
		}
		if(num2 == null || num2 == "") {
			num2 += "1.00000";
		} else {
			num2 = parseFloat(num2);
		}
		var newNum = parseFloat(num1).add(parseFloat(num2));
		return newNum;
	};
	/**
	 *计算主数量 
	 */
	NCMMPubUtil.getNum = function(nastnum, vchange) {
		var num = nastnum;

		if(nastnum == null || nastnum == "") {
			nastnum += "00.00000";
		} else {
			nastnum = parseFloat(nastnum);
		}
		if(vchange == null || vchange == "") {
			vchange += "1.00000";
		} else {
			var crArray = vchange.split("/");
			if(crArray == null || crArray.length != 2) {
				return num;
			}
			var cru = parseFloat(crArray[0]);
			var crd = parseFloat(crArray[1]);
			if(cru == null || crd == null) {
				return num;
			}
			num = (cru * nastnum) / crd;
			if(!isBitEnByBitNum(num, 8)) {
				num = num.toFixed(8);
			}
		}

		return num;
	}
	NCMMPubUtil.getSubNum = function(arg1, arg2) {

			var r1, r2, m, n;
			try {
				r1 = arg1.toString().split(".")[1].length
			} catch(e) {
				r1 = 0
			}
			try {
				r2 = arg2.toString().split(".")[1].length
			} catch(e) {
				r2 = 0
			}
			m = Math.pow(10, Math.max(r1, r2));
			//动态控制精度长度
			n = (r1 >= r2) ? r1 : r2;
			//$alert("m="+m.toString+" n="+n.toString()+" arg1-arg2="+((arg1*m-arg2*m)/m).toString());
			return((arg1 * m - arg2 * m) / m).toFixed(n);
		}
		/**
		 *如果小数点位数多余某位，则四舍五入截断为bitnum位
		 */
	NCMMPubUtil.decimalPrecision = function(num, bitnumber) {

			var floatNum = parseFloat(num);
			if(isNaN(floatNum) || isNaN(bitnumber)) {
				return "NaN";
			}
			if(bitnumber == 0) {
				return parseInt(floatNum);
			}
			bitnumber = parseFloat(bitnumber);
			//实现四舍五入 比如两个精度 先乘以100再除以100
			var t = 1;
			var tmpbitnumber = bitnumber;
			for(; tmpbitnumber > 0; t *= 10, tmpbitnumber--);
			floatNum = Math.round(floatNum * t) / t;
			return floatNum;
		}
		/**
		 *主数量除以换算率得辅数量 
		 * @param {Object} nnum
		 * @param {Object} vchange
		 */
	NCMMPubUtil.getAstnum = function(nnum, vchange) {
		var nastum = nnum;
		if(nnum == null || nnum == "") {
			nnum += "00.00000";
		} else {
			nnum = parseFloat(nnum);
		}
		if(vchange == null || vchange == "") {
			vchange += "1.00000";
		} else {
			var crArray = vchange.split("/");
			if(crArray == null || crArray.length != 2) {
				return nastum;
			}
			var cru = parseFloat(crArray[0]);
			var crd = parseFloat(crArray[1]);
			if(cru == null || crd == null) {
				return nastum;
			}
			nastum = (crd * nnum) / cru;
			if(!isBitEnByBitNum(nastum, 8)) {
				nastum = nastum.toFixed(8);
			}
		}

		return nastum;
	}

	function isBitEnByBitNum(num, bitnum) {
		var rtn = false;
		var stringNum = num.toString();
		var bitNumberIndex = stringNum.indexOf('.');
		//整数
		if(bitNumberIndex < 0) {
			if(bitnum >= 0) {
				rtn = true;
			}
		} else if((stringNum.length - 1 - bitNumberIndex) <= bitnum) {
			rtn = true;
		}
		return rtn;
	}

} catch(e) {
	$e(e);
}